# *-*- coding:utf-8 -*-
# @Author  : ygree 
# @Time    : 2021/7/17 22:33
# @Software : RubyMine

=begin
  instance_methods(all:bool):列出对象(类)内部的方法
  respond_to?:调查对象的方法/属性是否可用
  send:执行对象的方法
=end
=begin
@x  instance variable//实例 self.
@@x class variable
$x  global variable
x   local variable

=end

class Player
  #存取器，即setter getter
  attr_accessor :name, :price
  attr_reader :name #getter

  def initialize(name = "ok", price = 0)
    #构造器函数
    # @name是实例变量，能够被该类或者子类继承使用。
    @name = name #this.name =name
    @price = price #this.name =name
    #不能直接调用 name 和 price 属性 ，ruby封装性,只能通过 setter getter--> attr_accessor

  end

  def ask
    puts "#{@name}  ask what?"
  end

  def showprice
    puts "#{self.name}:#{@price}" #self->object
  end

  # ----------class
  @@oop = "oop" #变量，也需要 getter setter 方法才能使用和修改
  OOP = 111 #常量,Player::OOP 获取
  # attr_accessor :@@oop 不能使用
  def self.getoop
    @@oop
  end

  def self.show #self->class
    # self.name,class名
    puts "#{self.name}:#{@name}:#{OOP}" #self->class
    puts "#{self.name}:#{@name}:#{@@oop}" #self->class

  end

  # 定义class 静态方法

  class << self
    def classDown01
      puts "#{self.name}:classDown01" #self->class

    end

    def classDown02
      puts "#{self.name}:classDown02" #self->class

    end

    def classDown03
      puts "#{self.name}:classDown03" #self->class

    end
  end
end

puts "-----Player------"
Player.show #Player::,class未实例化,自然也没有默认参数
Player.classDown01 #Player:classDown01

puts Player.getoop #oop
puts Player::OOP #111

puts "-----------"
p1 = Player.new
p1.ask
p2 = Player.new("PP")
p2.ask
p2.name = "p2" #修改属性
p2.ask
puts "------instance_methods-------"
#该类的所有方法,仅用户自定义
puts Player.instance_methods (false)
=begin
          ask
          showprice
          show
=end

#该类的所有方法，真所有
# puts Player.instance_methods (true)

puts "------respond_to?-------"
#   respond_to?:调查对象的方法/属性是否可用
puts Player.respond_to?("ask") #false
puts p1.respond_to?("ask") #true

if p2.respond_to?("showprice")
  p2.send("showprice") #p2:0 ，执行该方法
else
  puts "something wrong"
end
